clear all;
close all;
%r4r6_data_raw1=importdata('g:\Research_Data\talinr4r6\20140914_talinwtr4r6_bead001_s1_tbp_0.4pns_4pns_alternative_c180-f-unfoldingstep.dat');
%r4r6_data_raw2=importdata('g:\Research_Data\talinr4r6\20140920_talinwtr4r6_bead004_s1_tbp_0.4pns_4pns_alternative_c155-f-unfoldingstep.dat');
%r4r6_data_raw3=importdata('g:\Research_Data\talinr4r6\20140920_talinwtr4r6_bead003_s1_tbp__0.4pns_4pns_alternative_c176-f-unfoldingstep.dat');
r4r6_data_raw=importdata('g:\Research_Data\talinr4r6\goodunfolding.txt');

%r4r6_data=[r4r6_data_raw1.data;r4r6_data_raw2.data;r4r6_data_raw4.data];
r4r6_data=r4r6_data_raw.data;
r4r6_slow=r4r6_data(mod(r4r6_data(:,1),2)==1,:);
r4r6_fast=r4r6_data(mod(r4r6_data(:,1),2)==0,:);
r4r6_fast_bin=histc(r4r6_fast(:,3),0:1:30);
r4r6_slow_bin=histc(r4r6_slow(:,3),0:1:30);
%cd 'G:\Mingxi\Dropbox\Thesis Proposal\codes\trunk\parameterfiting'

r4r6_fast_hist=[0:1:30; r4r6_fast_bin'];
r4r6_slow_hist=[0:1:30; r4r6_slow_bin'];
%plot(r4r6_slow_hist');
transition_matrix=[0 1 0 0;0 0 1 0;0 0 0 1;0 0 0 0];


optimized_overall=find_unfolding_parameters_main_v4(transition_matrix',[0.36 3.37],[r4r6_slow_hist' r4r6_fast_hist(2,:)'],1:1:3,1:1:6,[0.00057348   0.080832  1.6471e-08      3      6      3.9907],[0.00000000001 0.00000000001 0.00000000001 0.2 0.2 0.2],[1 1 1 15 15 15],1,2);
%optimized_rates_fast=find_unfolding_parameters_main_v4(transition_matrix',4,r4r6_fast_hist',1:1:3,1:1:6,[9.32867848333985e-05 0.975380678522285 1.43307632071753e-05 2.83848882082712 4.11398871902754 2.07662901666580],[0.0000001 0.0000001 0.0000001 0.2 0.2 0.2],[1 1 1 15 15 15],0,2);
%optimized_rates_slow=find_unfolding_parameters_main_v4(transition_matrix',0.4,r4r6_slow_hist',1:1:3,1:1:6,[9.32867848333985e-05 0.975380678522285 1.43307632071753e-05 2.83848882082712 4.11398871902754 2.07662901666580],[0.0000001 0.0000001 0.0000001 0.2 0.2 0.2],[1 1 1 15 15 15],0,2);

%minimization_function_v4(optimized_rates_fast, transition_matrix', 4, r4r6_fast_hist', 1:1:3,1:1:6,  [9.32867848333985e-05 0.975380678522285 1.43307632071753e-05 2.83848882082712 4.11398871902754 2.07662901666580], 0)
minimization_function_v4(optimized_overall, transition_matrix', [0.36 3.37],[r4r6_slow_hist' r4r6_fast_hist(2,:)'], 1:1:3,1:1:6,  [0.00073069    0.007468  7.6544e-06      2.8438       6.045      2.5333], 1)